package org.ariadne_eu.hcifetcher.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

public class AddQuestionServlet extends HttpServlet{
	
	private static final long serialVersionUID = -3872068556615082331L;
	private static final Logger log = Logger.getLogger(AddQuestionServlet.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String author = req.getParameter("studentid");
		String questionBody = req.getParameter("question");
		String courseID = req.getParameter("courseid");
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity question  = new Entity("Question");
		question.setProperty("studentid", author);
		question.setProperty("question", questionBody);
		question.setProperty("datestamp", new Date());
		question.setProperty("courseid", courseID);
		Key k = datastore.put(question);
		resp.getWriter().write("Added question by " + author + " with body: \"" + questionBody + "\" to course: " + courseID + ". Key: " + k);
		log.log(Level.INFO, "Added question by " + author + " with body: \"" + questionBody + "\" to course: " + courseID + ". Key: " + k);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doGet(req, resp);
	}

}
